package com.qs.dao;

import java.util.List;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qs.entities.Channel;
import com.qs.entities.Feed;

public class FeedDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory.getLogger(FeedDAO.class);
	
	// property constants
	public static final String LANGUAGE = "language";
	public static final String TEXT = "text";

	public void save(Feed transientInstance) {
		log.debug("saving Feed instance");
		Session session = null;
    	Transaction tx = null;
		try {
			session = getSession();
			tx 		= session.beginTransaction();
			session.save(transientInstance);
			log.debug("save successful");
			tx.commit();
		} catch (RuntimeException re) {
			log.error("save failed", re);
			try{
				tx.rollback();
			}
			catch(Exception rbe){
				log.error("Couldn't roll back transaction", rbe);
			}
			throw re;
		}
	}

	public void delete(Feed persistentInstance) {
		log.debug("deleting Feed instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Feed findById(java.lang.String id) {
		log.debug("getting Feed instance with id: " + id);
		try {
			Feed instance = (Feed) getSession().get("Feed", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Feed instance) {
		log.debug("finding Feed instance by example");
		try {
			List results = getSession().createCriteria("Feed")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Feed instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Feed as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByLanguage(Object language) {
		return findByProperty(LANGUAGE, language);
	}

	public List findByText(Object text) {
		return findByProperty(TEXT, text);
	}

	public List findAll() {
		log.debug("finding all Feed instances");
		try {
			String queryString = "from Feed";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List<Feed> findAllByLimit(int offset,int limit) {
		log.debug("finding all Feed instances");
		try {
			String queryString = "from Feed order by createdOn";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setFirstResult(offset);
			queryObject.setMaxResults(limit);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List<Feed> findAllByLimit(int offset,int limit,Channel channel) {
		log.debug("finding all Feed instances");
		try {
			String queryString = "from Feed where channel=:channelObj";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter("channelObj", channel);
			queryObject.setFirstResult(offset);
			queryObject.setMaxResults(limit);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Feed merge(Feed detachedInstance) {
		log.debug("merging Feed instance");
		try {
			Feed result = (Feed) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Feed instance) {
		log.debug("attaching dirty Feed instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Feed instance) {
		log.debug("attaching clean Feed instance");
		try {
			getSession().buildLockRequest(LockOptions.NONE).lock(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}